package cn.tedu.order.webapi.service.impl;

import cn.tedu.cart.service.ICartService;
import cn.tedu.commons.pojo.order.dto.OrderAddDTO;
import cn.tedu.commons.pojo.order.model.Order;
import cn.tedu.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.order.service.IOrderService;
import cn.tedu.order.webapi.mapper.OrderMapper;
import cn.tedu.stock.service.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@DubboService
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @DubboReference
    //当前业务逻辑层要调用库存模块的减少库存方法
    private IStockService dubboStockService;

    @DubboReference
    //当前业务逻辑层要调用购物车模块中删除购物车中商品的方法
    private ICartService dubboCartService;

    @Override
    public void orderAdd(OrderAddDTO orderAddDTO) {

        //实例化减少库存需要的对象
        StockReduceCountDTO stockReduceCountDTO = new StockReduceCountDTO();
        //赋值减少库存数
        stockReduceCountDTO.setReduceCount(orderAddDTO.getCount());
        //赋值减少商品数
        stockReduceCountDTO.setCommodityCode(orderAddDTO.getCommodityCode());
        // 这里新增订单要先减少库存(要调用stock项目的方法)
        dubboStockService.reduceCommodityCount(stockReduceCountDTO);
        // 再删除购物车中的商品(要调用cart项目的方法)
        dubboCartService.deleteUserCart(orderAddDTO.getUserId(),orderAddDTO.getCommodityCode());
        // 上面的操作后面微服务学习后会实现,现在暂时值新增订单信息
        // 实例化订单对象
        Order order = new Order();
        //同名属性复制
        BeanUtils.copyProperties(orderAddDTO,order);
        orderMapper.insertOrder(order);
        log.info("新增订单完成！{}",order);
    }
}
